package com.css.zfzx.fwsjcj.modules.statistics.fw.service.impl;

import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.bpm.platform.utils.UUIDGenerator;
import com.css.zfzx.fwsjcj.modules.statistics.fw.constant.FwConstants;
import com.css.zfzx.fwsjcj.modules.statistics.fw.constant.StructureConstants;
import com.css.zfzx.fwsjcj.modules.statistics.fw.entity.FwAreaBuildingEntity;
import com.css.zfzx.fwsjcj.modules.statistics.fw.entity.FwAreaBuildingTemporaryEntity;
import com.css.zfzx.fwsjcj.modules.statistics.fw.repository.FwAreaBuildingReqository;
import com.css.zfzx.fwsjcj.modules.statistics.fw.repository.FwAreaBuildingTemporaryReqository;
import com.css.zfzx.fwsjcj.modules.statistics.fw.repository.FwDivisionAreaBuildingNativReqository;
import com.css.zfzx.fwsjcj.modules.statistics.fw.service.FwDivisionAreaBuildingService;
import com.css.zfzx.fwsjcj.modules.statistics.fw.util.Util;
import com.css.zfzx.fwsjcj.modules.statistics.fw.vo.ProvinceParams;
import com.css.zfzx.fwsjcj.modules.statistics.fw.vo.SelectChinaAndProvinceVo;
import com.css.zfzx.fwsjcj.modules.statistics.fw.vo.SelectStructureVo;
import com.css.zfzx.fwsjcj.modules.statistics.fw.vo.SelectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class FwDivisionAreaBuildingServiceImpl implements FwDivisionAreaBuildingService {

    @Autowired
    private FwDivisionAreaBuildingNativReqository fwDivisionAreaBuildingNativReqository;

    @Autowired
    private FwAreaBuildingTemporaryReqository fwAreaBuildingTemporaryReqository;

    @Autowired
    private FwAreaBuildingReqository fwAreaBuildingReqository;

    @Override
    public String getDivisionAreaBuilding(Date date) throws IllegalAccessException {
        try {
            String userId = PlatformSessionUtils.getUserId();
            FwConstants fwConstants = new FwConstants();
            Field[] fields = fwConstants.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                String province = fields[i].get(fwConstants).toString();//省名称
                System.err.println("开始获取"+province+"数据----------------");
                //砖混结构抽样面积及栋数
                List<SelectVo> zhList = fwDivisionAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType(province,"0");
                Double zhAllSamplingArea = 0.0;
                int zhAllSamplingBuilding = 0;
                for (SelectVo zh : zhList) {
                    zhAllSamplingArea = zh.getSamplingArea();
                    zhAllSamplingBuilding = zh.getSamplingBuilding();
                }
                //多层钢筋混凝土结构抽样面积及栋数
                List<SelectVo> dcList = fwDivisionAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType(province,"1");
                Double dcAllSamplingArea = 0.0;
                int dcAllSamplingBuilding = 0;
                for (SelectVo dc : dcList) {
                    dcAllSamplingArea = dc.getSamplingArea();
                    dcAllSamplingBuilding = dc.getSamplingBuilding();
                }
                //高层结构抽样面积及栋数
                List<SelectVo> gcList = fwDivisionAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType(province,"2");
                Double gcAllSamplingArea = 0.0;
                int gcAllSamplingBuilding = 0;
                for (SelectVo gc : gcList) {
                    gcAllSamplingArea = gc.getSamplingArea();
                    gcAllSamplingBuilding = gc.getSamplingBuilding();
                }
                //工业厂房结构抽样面积及栋数
                List<SelectVo> gyList = fwDivisionAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType(province,"3");
                Double gyAllSamplingArea = 0.0;
                int gyAllSamplingBuilding = 0;
                for (SelectVo gy : gyList) {
                    gyAllSamplingArea = gy.getSamplingArea();
                    gyAllSamplingBuilding = gy.getSamplingBuilding();
                }
                //空旷结构 抽样面积及栋数
                List<SelectVo> kkList = fwDivisionAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType(province,"4");
                Double kkAllSamplingArea = 0.0;
                int kkAllSamplingBuilding = 0;
                for (SelectVo kk : kkList) {
                    kkAllSamplingArea = kk.getSamplingArea();
                    kkAllSamplingBuilding = kk.getSamplingBuilding();
                }
                //土木结构 抽样面积及栋数
                List<SelectVo> tmList = fwDivisionAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType(province,"5");
                Double tmAllSamplingArea = 0.0;
                int tmAllSamplingBuilding = 0;
                for (SelectVo tm : tmList) {
                    tmAllSamplingArea = tm.getSamplingArea();
                    tmAllSamplingBuilding = tm.getSamplingBuilding();
                }
                //木结构 抽样面积及栋数
                List<SelectVo> mList = fwDivisionAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType(province,"6");
                Double mAllSamplingArea = 0.0;
                int mAllSamplingBuilding = 0;
                for (SelectVo m : mList) {
                    mAllSamplingArea = m.getSamplingArea();
                    mAllSamplingBuilding = m.getSamplingBuilding();
                }
                //石结构 抽样面积及栋数
                List<SelectVo> sList = fwDivisionAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType(province,"7");
                Double sAllSamplingArea = 0.0;
                int sAllSamplingBuilding = 0;
                for (SelectVo s : sList) {
                    sAllSamplingArea = s.getSamplingArea();
                    sAllSamplingBuilding = s.getSamplingBuilding();
                }
                //其他 抽样面积及栋数
                List<SelectVo> qtList = fwDivisionAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType(province,"8");
                Double qtAllSamplingArea = 0.0;
                int qtAllSamplingBuilding = 0;
                for (SelectVo qt : qtList) {
                    qtAllSamplingArea = qt.getSamplingArea();
                    qtAllSamplingBuilding = qt.getSamplingBuilding();
                }
                //砖木结构抽样面积及栋数
                List<SelectVo> zmList = fwDivisionAreaBuildingNativReqository.getSamplingAreaAndBuildingByStructureType(province,"9");
                Double zmAllSamplingArea = 0.0;
                int zmAllSamplingBuilding = 0;
                for (SelectVo zm : zmList) {
                    zmAllSamplingArea = zm.getSamplingArea();
                    zmAllSamplingBuilding = zm.getSamplingBuilding();
                }

                //全省抽样面积及抽样栋数
                List<SelectVo> allAreaAndBuildingCount = fwDivisionAreaBuildingNativReqository.getAllAreaAndBuildingCount(province);
                Double allSamplingArea = 0.0;
                int allSamplingBuilding = 0;
                for (SelectVo all : allAreaAndBuildingCount) {
                    allSamplingArea = all.getSamplingArea();
                    allSamplingBuilding = all.getSamplingBuilding();
                }

                //砖混结构抽样面积抽样率及栋数占比
                String zhSamplingArea = Util.parsePercent(zhAllSamplingArea, allSamplingArea);
                String zhSamplingBuilding = Util.parsePercent(zhAllSamplingBuilding, allSamplingBuilding);

                //多层钢筋混凝土结构 抽样面积抽样率及栋数占比
                String dcSamplingArea = Util.parsePercent(dcAllSamplingArea, allSamplingArea);
                String dcSamplingBuilding = Util.parsePercent(dcAllSamplingBuilding, allSamplingBuilding);

                //高层结构抽样面积抽样率及栋数占比
                String gcSamplingArea = Util.parsePercent(gcAllSamplingArea, allSamplingArea);
                String gcSamplingBuilding = Util.parsePercent(gcAllSamplingBuilding, allSamplingBuilding);

                //工业厂房结构抽样面积抽样率及栋数占比
                String gySamplingArea = Util.parsePercent(gyAllSamplingArea, allSamplingArea);
                String gySamplingBuilding = Util.parsePercent(gyAllSamplingBuilding, allSamplingBuilding);

                //空旷结构抽样面积抽样率及栋数占比
                String kkSamplingArea = Util.parsePercent(kkAllSamplingArea, allSamplingArea);
                String kkSamplingBuilding = Util.parsePercent(kkAllSamplingBuilding, allSamplingBuilding);

                //土木结构 抽样面积抽样率及栋数占比
                String tmSamplingArea = Util.parsePercent(tmAllSamplingArea, allSamplingArea);
                String tmSamplingBuilding = Util.parsePercent(tmAllSamplingBuilding, allSamplingBuilding);

                //木结构 抽样面积抽样率及栋数占比
                String mSamplingArea = Util.parsePercent(mAllSamplingArea, allSamplingArea);
                String mSamplingBuilding = Util.parsePercent(mAllSamplingBuilding, allSamplingBuilding);

                //石结构 抽样面积抽样率及栋数占比
                String sSamplingArea = Util.parsePercent(sAllSamplingArea, allSamplingArea);
                String sSamplingBuilding = Util.parsePercent(sAllSamplingBuilding, allSamplingBuilding);

                //其他结构 抽样面积抽样率及栋数占比
                String qtSamplingArea = Util.parsePercent(qtAllSamplingArea, allSamplingArea);
                String qtSamplingBuilding = Util.parsePercent(qtAllSamplingBuilding, allSamplingBuilding);

                //砖木结构抽样面积抽样率及栋数占比
                String zmSamplingArea = Util.parsePercent(zmAllSamplingArea, allSamplingArea);
                String zmSamplingBuilding = Util.parsePercent(zmAllSamplingBuilding, allSamplingBuilding);

                //保存砖混结构
                FwAreaBuildingTemporaryEntity zhFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
                zhFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
                zhFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
                zhFwAreaBuildingTemporaryEntity.setDistrictName(province);//省（直辖市）名称
                zhFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
                zhFwAreaBuildingTemporaryEntity.setStructureName("砖混结构");//结构类型
                zhFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(zhAllSamplingArea));//结构面积
                zhFwAreaBuildingTemporaryEntity.setStructureAreaProportion(zhSamplingArea);//面积占比
                zhFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(zhAllSamplingBuilding));//结构栋数
                zhFwAreaBuildingTemporaryEntity.setStructureCountProportion(zhSamplingBuilding);//栋数占比
                zhFwAreaBuildingTemporaryEntity.setDelFlag("0");
                zhFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
                zhFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
                fwAreaBuildingTemporaryReqository.save(zhFwAreaBuildingTemporaryEntity);
                log.info(province+"---"+"砖混结构相关数据保存成功--------");
                //保存多层钢筋混凝土结构
                FwAreaBuildingTemporaryEntity dcFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
                dcFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
                dcFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
                dcFwAreaBuildingTemporaryEntity.setDistrictName(province);//省（直辖市）名称
                dcFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
                dcFwAreaBuildingTemporaryEntity.setStructureName("多层钢筋混凝土结构");//结构类型
                dcFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(dcAllSamplingArea));//结构面积
                dcFwAreaBuildingTemporaryEntity.setStructureAreaProportion(dcSamplingArea);//面积占比
                dcFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(dcAllSamplingBuilding));//结构栋数
                dcFwAreaBuildingTemporaryEntity.setStructureCountProportion(dcSamplingBuilding);//栋数占比
                dcFwAreaBuildingTemporaryEntity.setDelFlag("0");
                dcFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
                dcFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
                fwAreaBuildingTemporaryReqository.save(dcFwAreaBuildingTemporaryEntity);
                log.info(province+"---"+"多层钢筋混凝土结构相关数据保存成功--------");
                //高层结构抽样面积及栋数
                FwAreaBuildingTemporaryEntity gcFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
                gcFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
                gcFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
                gcFwAreaBuildingTemporaryEntity.setDistrictName(province);//省（直辖市）名称
                gcFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
                gcFwAreaBuildingTemporaryEntity.setStructureName("高层结构");//结构类型
                gcFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(gcAllSamplingArea));//结构面积
                gcFwAreaBuildingTemporaryEntity.setStructureAreaProportion(gcSamplingArea);//面积占比
                gcFwAreaBuildingTemporaryEntity.setStructureCountData(gcAllSamplingBuilding);//结构栋数
                gcFwAreaBuildingTemporaryEntity.setStructureCountProportion(gcSamplingBuilding);//栋数占比
                gcFwAreaBuildingTemporaryEntity.setDelFlag("0");
                gcFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
                gcFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
                fwAreaBuildingTemporaryReqository.save(gcFwAreaBuildingTemporaryEntity);
                log.info(province+"---"+"高层结构相关数据保存成功--------");
                //工业厂房结构
                FwAreaBuildingTemporaryEntity gyFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
                gyFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
                gyFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
                gyFwAreaBuildingTemporaryEntity.setDistrictName(province);//省（直辖市）名称
                gyFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
                gyFwAreaBuildingTemporaryEntity.setStructureName("工业厂房结构");//结构类型
                gyFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(gyAllSamplingArea));//结构面积
                gyFwAreaBuildingTemporaryEntity.setStructureAreaProportion(gySamplingArea);//面积占比
                gyFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(gyAllSamplingBuilding));//结构栋数
                gyFwAreaBuildingTemporaryEntity.setStructureCountProportion(gySamplingBuilding);//栋数占比
                gyFwAreaBuildingTemporaryEntity.setDelFlag("0");
                gyFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
                gyFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
                fwAreaBuildingTemporaryReqository.save(gyFwAreaBuildingTemporaryEntity);
                log.info(province+"---"+"工业厂房结构相关数据保存成功--------");
                //空旷结构
                FwAreaBuildingTemporaryEntity kkFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
                kkFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
                kkFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
                kkFwAreaBuildingTemporaryEntity.setDistrictName(province);//省（直辖市）名称
                kkFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
                kkFwAreaBuildingTemporaryEntity.setStructureName("空旷结构");//结构类型
                kkFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(kkAllSamplingArea));//结构面积
                kkFwAreaBuildingTemporaryEntity.setStructureAreaProportion(kkSamplingArea);//面积占比
                kkFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(kkAllSamplingBuilding));//结构栋数
                kkFwAreaBuildingTemporaryEntity.setStructureCountProportion(kkSamplingBuilding);//栋数占比
                kkFwAreaBuildingTemporaryEntity.setDelFlag("0");
                kkFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
                kkFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
                fwAreaBuildingTemporaryReqository.save(kkFwAreaBuildingTemporaryEntity);
                log.info(province+"---"+"空旷结构相关数据保存成功--------");
                //土木结构
                FwAreaBuildingTemporaryEntity tmFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
                tmFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
                tmFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
                tmFwAreaBuildingTemporaryEntity.setDistrictName(province);//省（直辖市）名称
                tmFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
                tmFwAreaBuildingTemporaryEntity.setStructureName("土木结构");//结构类型
                tmFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(tmAllSamplingArea));//结构面积
                tmFwAreaBuildingTemporaryEntity.setStructureAreaProportion(tmSamplingArea);//面积占比
                tmFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(tmAllSamplingBuilding));//结构栋数
                tmFwAreaBuildingTemporaryEntity.setStructureCountProportion(tmSamplingBuilding);//栋数占比
                tmFwAreaBuildingTemporaryEntity.setDelFlag("0");
                tmFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
                tmFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
                fwAreaBuildingTemporaryReqository.save(tmFwAreaBuildingTemporaryEntity);
                log.info(province+"---"+"土木结构相关数据保存成功--------");
                //木结构
                FwAreaBuildingTemporaryEntity mFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
                mFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
                mFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
                mFwAreaBuildingTemporaryEntity.setDistrictName(province);//省（直辖市）名称
                mFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
                mFwAreaBuildingTemporaryEntity.setStructureName("木结构");//结构类型
                mFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(mAllSamplingArea));//结构面积
                mFwAreaBuildingTemporaryEntity.setStructureAreaProportion(mSamplingArea);//面积占比
                mFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(mAllSamplingBuilding));//结构栋数
                mFwAreaBuildingTemporaryEntity.setStructureCountProportion(mSamplingBuilding);//栋数占比
                mFwAreaBuildingTemporaryEntity.setDelFlag("0");
                mFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
                mFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
                fwAreaBuildingTemporaryReqository.save(mFwAreaBuildingTemporaryEntity);
                log.info(province+"---"+"木结构相关数据保存成功--------");
                //石结构
                FwAreaBuildingTemporaryEntity sFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
                sFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
                sFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
                sFwAreaBuildingTemporaryEntity.setDistrictName(province);//省（直辖市）名称
                sFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
                sFwAreaBuildingTemporaryEntity.setStructureName("石结构");//结构类型
                sFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(sAllSamplingArea));//结构面积
                sFwAreaBuildingTemporaryEntity.setStructureAreaProportion(sSamplingArea);//面积占比
                sFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(sAllSamplingBuilding));//结构栋数
                sFwAreaBuildingTemporaryEntity.setStructureCountProportion(sSamplingBuilding);//栋数占比
                sFwAreaBuildingTemporaryEntity.setDelFlag("0");
                sFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
                sFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
                fwAreaBuildingTemporaryReqository.save(sFwAreaBuildingTemporaryEntity);
                log.info(province+"---"+"石结构相关数据保存成功--------");
                //其他
                FwAreaBuildingTemporaryEntity qtFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
                qtFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
                qtFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
                qtFwAreaBuildingTemporaryEntity.setDistrictName(province);//省（直辖市）名称
                qtFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
                qtFwAreaBuildingTemporaryEntity.setStructureName("其他");//结构类型
                qtFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(qtAllSamplingArea));//结构面积
                qtFwAreaBuildingTemporaryEntity.setStructureAreaProportion(qtSamplingArea);//面积占比
                qtFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(qtAllSamplingBuilding));//结构栋数
                qtFwAreaBuildingTemporaryEntity.setStructureCountProportion(qtSamplingBuilding);//栋数占比
                qtFwAreaBuildingTemporaryEntity.setDelFlag("0");
                qtFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
                qtFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
                fwAreaBuildingTemporaryReqository.save(qtFwAreaBuildingTemporaryEntity);
                log.info(province+"---"+"其他相关数据保存成功--------");
                //保存砖木结构
                FwAreaBuildingTemporaryEntity zmFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
                zmFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
                zmFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
                zmFwAreaBuildingTemporaryEntity.setDistrictName(province);//省（直辖市）名称
                zmFwAreaBuildingTemporaryEntity.setIsStructure("1");//是否有结构类型
                zmFwAreaBuildingTemporaryEntity.setStructureName("砖木结构");//结构类型
                zmFwAreaBuildingTemporaryEntity.setStructureAreaData(Util.div(zmAllSamplingArea));//结构面积
                zmFwAreaBuildingTemporaryEntity.setStructureAreaProportion(zmSamplingArea);//面积占比
                zmFwAreaBuildingTemporaryEntity.setStructureCountData((int) Math.round(zmAllSamplingBuilding));//结构栋数
                zmFwAreaBuildingTemporaryEntity.setStructureCountProportion(zmSamplingBuilding);//栋数占比
                zmFwAreaBuildingTemporaryEntity.setDelFlag("0");
                zmFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
                zmFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
                fwAreaBuildingTemporaryReqository.save(zmFwAreaBuildingTemporaryEntity);
                log.info(province+"---"+"砖木结构相关数据保存成功--------");
                //根据省名称获取该省下所有市
                List<Map<String, Object>> divisions = fwDivisionAreaBuildingNativReqository.getDivisions(province);
                for (int k = 0; k < divisions.size(); k++) {
                    if (divisions.get(k).get("districtName") != null) {
                        String districtName = divisions.get(k).get("districtName").toString();//市名称
                        System.err.println("开始获取 "+province+"  "+districtName+" 数据----");
                        //各市总面积
                        String areaIsOpen = fwDivisionAreaBuildingNativReqository.getAreaIsOpen(province, districtName);//查询总面积修正值是否启用 area_is_open
                        Double cityAllArea = 0.0;
                        if (areaIsOpen.equals("0")) {
                            cityAllArea = fwDivisionAreaBuildingNativReqository.getAllAmendatoryAreaByProvince(province, districtName);//修正面积
                        } else {
                            cityAllArea = fwDivisionAreaBuildingNativReqository.getAllActualAreaByProvince(province, districtName);//实际面积
                        }
                        //转换为平方米
                        cityAllArea = Util.mul(cityAllArea);

                        //各市总栋数
                        String countIsOpen = fwDivisionAreaBuildingNativReqository.getCountIsOpen(province, districtName);//查询总栋数修正值是否启用 count_is_open
                        int cityAllBuilding = 0;
                        if (countIsOpen.equals("0")) {
                            cityAllBuilding = fwDivisionAreaBuildingNativReqository.getAllAmendatoryBuildingByProvince(province, districtName);//修正栋数
                        } else {
                            cityAllBuilding = fwDivisionAreaBuildingNativReqository.getAllActualBuildingByProvince(province, districtName);//实际栋数
                        }
                        //各市抽样面积及抽样栋数
                        List<SelectVo> lists = fwDivisionAreaBuildingNativReqository.getSamplingAreaAndBuilding(province, districtName);
                        Double citySamplingArea = 0.0;
                        int citySamplingBuilding = 0;
                        for (SelectVo proSamplingAreaAndBuilding : lists) {
                            citySamplingArea = proSamplingAreaAndBuilding.getSamplingArea();
                            citySamplingBuilding = proSamplingAreaAndBuilding.getSamplingBuilding();
                        }
                        //各市面积抽样率
                        String cityAreaSampling = Util.parsePercent(citySamplingArea, cityAllArea);
                        //各市栋数抽样率
                        String cityBuildingSampling = Util.parsePercent(citySamplingBuilding, cityAllBuilding);
                        //获取市编码
                        String divisionCode = fwDivisionAreaBuildingNativReqository.getDivisionCode(districtName);
                        FwAreaBuildingTemporaryEntity cityFwAreaBuildingTemporaryEntity = new FwAreaBuildingTemporaryEntity();
                        cityFwAreaBuildingTemporaryEntity.setId(UUIDGenerator.getUUID());
                        cityFwAreaBuildingTemporaryEntity.setDataTime(date);//数据日期
                        cityFwAreaBuildingTemporaryEntity.setDistrictName(province);//省（直辖市）名称
                        cityFwAreaBuildingTemporaryEntity.setBelongtoCity(districtName);//市名称
                        cityFwAreaBuildingTemporaryEntity.setDivisionCode(divisionCode);//编码
                        cityFwAreaBuildingTemporaryEntity.setDivisionLevel("2");
                        cityFwAreaBuildingTemporaryEntity.setAreaData(Util.div(citySamplingArea));//省（直辖市）面积
                        cityFwAreaBuildingTemporaryEntity.setAreaSampling(cityAreaSampling);//面积抽样率
                        cityFwAreaBuildingTemporaryEntity.setCountData((int) Math.round(citySamplingBuilding));//省（直辖市）栋数
                        cityFwAreaBuildingTemporaryEntity.setCountSampling(cityBuildingSampling);//栋数抽样率
                        cityFwAreaBuildingTemporaryEntity.setIsStructure("0");//是否有结构类型
                        cityFwAreaBuildingTemporaryEntity.setDelFlag("0");
                        cityFwAreaBuildingTemporaryEntity.setCreatorId(userId);//创建人
                        cityFwAreaBuildingTemporaryEntity.setCreateTime(date);//创建时间
                        fwAreaBuildingTemporaryReqository.save(cityFwAreaBuildingTemporaryEntity);
                        log.info("---" + province + "---" + districtName+ " 数据保存成功----------");
                    }
                }
            }
            return "success";
        }catch (Exception e) {
                e.printStackTrace();
                log.error("获取市数据失败");
                return "error";
            }
    }

    @Override
    public List<SelectChinaAndProvinceVo> getProvinceFwData(String province) {
        List<SelectChinaAndProvinceVo> provinceFwData = fwDivisionAreaBuildingNativReqository.getProvinceFwData(province);
        int size = provinceFwData.size();
        if (size < 1) {
            SelectChinaAndProvinceVo selectChinaAndProvinceVo = new SelectChinaAndProvinceVo();
            selectChinaAndProvinceVo.setId(UUIDGenerator.getUUID());
            selectChinaAndProvinceVo.setDataTime(null);//数据日期
            selectChinaAndProvinceVo.setDistrictName(province);//省（直辖市）名称
            selectChinaAndProvinceVo.setAreaData(0.0);//省（直辖市）面积
            selectChinaAndProvinceVo.setAreaSampling("0%"); //面积抽样率
            selectChinaAndProvinceVo.setCountData(0);//省（直辖市）栋数
            selectChinaAndProvinceVo.setCountSampling("0%");//栋数抽样率
            provinceFwData.add(selectChinaAndProvinceVo);
        }
        return provinceFwData;
    }

    @Override
    public List<SelectStructureVo> getProvinceStructureData(String province) throws IllegalAccessException {
        List<SelectStructureVo> structureData = fwDivisionAreaBuildingNativReqository.getProvinceStructureData(province);
        ArrayList<Object> arrayList = new ArrayList<>();
        for(SelectStructureVo structure:structureData){
            String structureName = structure.getStructureName();
            arrayList.add(structureName);
        }
        StructureConstants structureConstants = new StructureConstants();
        Field[] fields = structureConstants.getClass().getDeclaredFields();
        for (Field field : fields) {
            String structure = field.get(structureConstants).toString();
            boolean contains = arrayList.contains(structure);
            if(!contains){
                SelectStructureVo selectStructureVo = new SelectStructureVo();
                selectStructureVo.setId(UUIDGenerator.getUUID());
                selectStructureVo.setDataTime(null);//数据日期
                selectStructureVo.setStructureName(structure);//结构类型
                selectStructureVo.setStructureAreaData(0.0);//结构面积
                selectStructureVo.setStructureAreaProportion("0%");//面积占比
                selectStructureVo.setStructureCountData(0);//结构栋数
                selectStructureVo.setStructureCountProportion("0%");//栋数占比
                structureData.add(selectStructureVo);
            }
        }
        return structureData;
    }

    @Override
    public List<SelectChinaAndProvinceVo> getCityFwData(String province) {
        List<SelectChinaAndProvinceVo> cityFwData = fwDivisionAreaBuildingNativReqository.getCityFwData(province);
        //获取省下的所有市
        List<Map<String, Object>> cityNames = fwDivisionAreaBuildingNativReqository.getCityNameByProvince(province);
        ArrayList<Object> arrayList = new ArrayList<>();
        for(SelectChinaAndProvinceVo cityVo:cityFwData){
            cityVo.setName(cityVo.getBelongtoCity());
            cityVo.setValue(cityVo.getAreaData());
            String districtName = cityVo.getDistrictName();
            arrayList.add(districtName);
        }
        for (int i = 0; i < cityNames.size(); i++) {
            String divisionName = cityNames.get(i).get("divisionName").toString();
            boolean contains = arrayList.contains(divisionName);
            if(!contains){
                SelectChinaAndProvinceVo selectChinaAndProvinceVo = new SelectChinaAndProvinceVo();
                selectChinaAndProvinceVo.setId(UUIDGenerator.getUUID());
                selectChinaAndProvinceVo.setDataTime(null);//数据日期
                selectChinaAndProvinceVo.setDistrictName(province);//省（直辖市）名称
                selectChinaAndProvinceVo.setBelongtoCity(divisionName);
                selectChinaAndProvinceVo.setAreaData(0.0);//省（直辖市）面积
                selectChinaAndProvinceVo.setAreaSampling("0%");//面积抽样率
                selectChinaAndProvinceVo.setCountData(0);//省（直辖市）栋数
                selectChinaAndProvinceVo.setCountSampling("0%");//栋数抽样率
                selectChinaAndProvinceVo.setName(divisionName);
                selectChinaAndProvinceVo.setValue(0.0);
                cityFwData.add(selectChinaAndProvinceVo);
            }
        }
        return cityFwData;
    }

    @Override
    public List<ProvinceParams> getProvinceCityFwData(String province) {
        List<ProvinceParams> list = fwDivisionAreaBuildingNativReqository.getProvinceCityFwData(province);
        //获取省下的所有市
        List<Map<String, Object>> cityNames = fwDivisionAreaBuildingNativReqository.getCityNameByProvince(province);
        ArrayList<Object> arrayList = new ArrayList<>();
        for(ProvinceParams cityDate:list){
            String belongtoCity = cityDate.getBelongtoCity();
            arrayList.add(belongtoCity);
        }
        for (int i = 0; i < cityNames.size(); i++) {
            String cityName = cityNames.get(i).get("divisionName").toString();
            boolean contains = arrayList.contains(cityName);
            if(!contains){
                ProvinceParams provinceParams = new ProvinceParams();
                provinceParams.setDistrictName(province);//省名称
                provinceParams.setBelongtoCity(cityName);//市名称
                provinceParams.setAllBuildingArea(0.0);//总面积实际值（万平方米）
                provinceParams.setAmendatoryAllBuildingArea(0.0);//总面积修正值（万平方米）
                provinceParams.setAreaIsOpen("");//总面积修正值是否启用
                provinceParams.setAllBuildingCount(0);//总栋数实际值
                provinceParams.setAmendatoryAllBuildingCount(0);//总栋数修正值
                provinceParams.setCountIsOpen("");//总栋数修正值是否启用
                list.add(provinceParams);
            }
        }
        return list;
    }

    @Override
    @Transactional
    public String updateDataByCity(ProvinceParams fwParams) {
        String districtName = fwParams.getDistrictName();
        String belongtoCity = fwParams.getBelongtoCity();
        Double allBuildingArea = fwParams.getAllBuildingArea();
        Double amendatoryAllBuildingArea = fwParams.getAmendatoryAllBuildingArea();
        String areaIsOpen = fwParams.getAreaIsOpen();
        int allBuildingCount = fwParams.getAllBuildingCount();
        int amendatoryAllBuildingCount = fwParams.getAmendatoryAllBuildingCount();
        String countIsOpen = fwParams.getCountIsOpen();
        String userId = PlatformSessionUtils.getUserId();
        Date date = new Date();
        //判断库中是否有该市的数据,有则更新数据,没有则新增一条数据
        int count = fwAreaBuildingReqository.queryFwDateByCity(districtName,belongtoCity);
        if(count == 0){
            FwAreaBuildingEntity fwAreaBuildingEntity = new FwAreaBuildingEntity();
            fwAreaBuildingEntity.setId(UUIDGenerator.getUUID());
            fwAreaBuildingEntity.setDistrictName(districtName);
            fwAreaBuildingEntity.setBelongtoCity(belongtoCity);
            String divisionCode = fwDivisionAreaBuildingNativReqository.getDivisionCode(belongtoCity);
            fwAreaBuildingEntity.setDivisionCode(divisionCode);
            fwAreaBuildingEntity.setDivisionLevel("2");
            fwAreaBuildingEntity.setAllBuildingArea(allBuildingArea);
            fwAreaBuildingEntity.setAmendatoryAllBuildingArea(amendatoryAllBuildingArea);
            fwAreaBuildingEntity.setAreaIsOpen(areaIsOpen);
            fwAreaBuildingEntity.setAllBuildingCount(allBuildingCount);
            fwAreaBuildingEntity.setAmendatoryAllBuildingCount(amendatoryAllBuildingCount);
            fwAreaBuildingEntity.setCountIsOpen(countIsOpen);
            fwAreaBuildingEntity.setDelFlag("0");
            fwAreaBuildingEntity.setCreatorId(userId);
            fwAreaBuildingEntity.setCreateTime(date);
            fwAreaBuildingReqository.save(fwAreaBuildingEntity);
        }else{
            fwAreaBuildingReqository.updateDataByCity(districtName, belongtoCity, allBuildingArea, amendatoryAllBuildingArea, areaIsOpen,
                    allBuildingCount, amendatoryAllBuildingCount, countIsOpen, userId, date);
        }
        return "修改成功";
    }

}
